package com.kitwee.serialport.util;

import java.util.Collection;
import java.util.Vector;

public class InstructionQueue<E> {

    private Vector<E> elements;

    private int index = 0;

    private int length;

    public InstructionQueue(final Collection<? extends E> coll) {
        elements = new Vector<>();
        elements.addAll(coll);
        length = elements.size();
    }

    public E get() {
        if (elements.isEmpty()) {
            return null;
        }
        return elements.get(index++ % length);
    }

    public E tail() {
        if (elements.isEmpty()) {
            return null;
        }
        return elements.get(length - 1);
    }

    public int length() {
        return length;
    }

    public void reset() {
        index = 0;
    }

}
